package com.example.encrption;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class MainActivity extends AppCompatActivity {
    //对称加密
    //DES(8) AES(16位) 3DES(24位)
    private EditText input, pw;
    //显示操作后的内容
    private TextView showInfo;
    String encryptResult = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initivw();

    }

    private void initivw() {
        input = (EditText) findViewById(R.id.input);
        pw = (EditText) findViewById(R.id.pw);
        showInfo = (TextView) findViewById(R.id.showInfo);
    }

    //加密解密的点击事件
    public void encrypt(View view) {
        String data = input.getText().toString().trim();
        String pw = this.pw.getText().toString().trim();

        String decryptResult = null;
        switch (view.getId()) {
            case R.id.desEncrypt://Des加密
                //需要类型，要加密的内容，需要密码
                encryptResult = encrypt("DES", data, pw);
                showInfo.setText("DES加密的内容是" + encryptResult);
                break;
            case R.id.desDecrypt://Des解密
                decryptResult = decrypt("DES", encryptResult, pw);
                showInfo.setText("DES解密的结果"+decryptResult);
                break;
            case R.id.desedeEncrypt://3Des加密
                //密码长度24位的

                encryptResult=encrypt("DESede",data,pw);
                showInfo.setText("3DES加密的结果"+decryptResult);
                break;
            case R.id.desdedDecrypt:
                decryptResult = decrypt("DESede", encryptResult, pw);
                showInfo.setText("3DES解密的结果"+decryptResult);
                break;
            case R.id.aesedeEncrypt://美国军方加密的工具
                encryptResult=encrypt("AESede",data,pw);
                showInfo.setText("aES加密的结果"+encryptResult);
                break;
            case R.id.aesdedDecrypt:
                decryptResult = decrypt("AESede", encryptResult, pw);
                showInfo.setText("3DES解密的结果"+decryptResult);
                break;
        }
    }

    private String decrypt(String type, String data, String pw) {
        switch (type) {
            case "DES":
                //1获取解密的对应的类.,加密引擎
                try {
                    Cipher cipher = Cipher.getInstance(type);
                    //2对加解密引擎进行初始化，制定密码
                    if (pw.getBytes().length != 8) {
                        throw new Exception("密码长度必须是8位的");
                    }
                    Key key = new SecretKeySpec(pw.getBytes(), type);
                    cipher.init(Cipher.DECRYPT_MODE, key);
                    //3执行解密
                    if (!TextUtils.isEmpty(data)) {
                        byte[] base64decode = Base64.decode(data.getBytes(), Base64.DEFAULT);
                        byte[] decryptBytes = cipher.doFinal(base64decode);

                        return new String(decryptBytes);
                    }


                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }


                break;
            case "DESede":
                break;
            case "AES":
                break;
            default:
                try {
                    throw new Exception("加密的类型不匹配，请选择");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
        return null;
    }


    //加密有三中方式：
    private String encrypt(String type, String data, String pw) {
switch (type){
    case "DES":
        //1获取加密的对应的类.,加密引擎
        try {
            Cipher cipher=Cipher.getInstance(type);
            //2对加密引擎进行初始化，制定密码
            if (pw.getBytes().length!=8){
                throw  new Exception("密码长度必须是8位的");
            }
            Key key=new SecretKeySpec(pw.getBytes(),type);
            cipher.init(Cipher.ENCRYPT_MODE,key);
            //3执行加密
              if (!TextUtils.isEmpty(data)){
               byte[] encryptBytes=   cipher.doFinal(data.getBytes());
                  byte[] base64 = Base64.encode(encryptBytes,Base64.DEFAULT);
                  return new String(base64);
              }


        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }


        break;
    case "DESede":
        try {
            Cipher cipher=Cipher.getInstance(type);
            //2对加密引擎进行初始化，制定密码
            if (pw.getBytes().length!=24){
                throw  new Exception("密码长度必须是8位的");
            }
            Key key=new SecretKeySpec(pw.getBytes(),type);
            cipher.init(Cipher.ENCRYPT_MODE,key);
            //3执行加密
            if (!TextUtils.isEmpty(data)){
                byte[] encryptBytes=   cipher.doFinal(data.getBytes());
                byte[] base64 = Base64.encode(encryptBytes,Base64.DEFAULT);
                return new String(base64);
            }


        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        break;
    case "AES":
        try {
            Cipher cipher=Cipher.getInstance(type);
            //2对加密引擎进行初始化，制定密码
            if (pw.getBytes().length!=16){
                throw  new Exception("密码长度必须是8位的");
            }
            Key key=new SecretKeySpec(pw.getBytes(),type);
            cipher.init(Cipher.ENCRYPT_MODE,key);
            //3执行加密
            if (!TextUtils.isEmpty(data)){
                byte[] encryptBytes=   cipher.doFinal(data.getBytes());
                byte[] base64 = Base64.encode(encryptBytes,Base64.DEFAULT);
                return new String(base64);
            }


        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }


        break;
     default:
         try {
             throw new Exception("加密的类型不匹配，请选择");
         } catch (Exception e) {
             e.printStackTrace();
         }
         break;

}


        return null;
    }
}
